package com.kuafood.flutter_kuafood_camera

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** FlutterKuafoodCameraPlugin */
class FlutterKuafoodCameraPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : MethodChannel


//    var activityBinding: ActivityPluginBinding? = null

  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.kuafood.kms.android/camera")
//    channel.setMethodCallHandler(this)

    var factory = KuaFoodCameraViewFactory(flutterPluginBinding.binaryMessenger);

    flutterPluginBinding.platformViewRegistry.registerViewFactory(VIEW_TYPE_ID, factory)
  }



  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "getPlatformVersion") {
      result.success("Android ${android.os.Build.VERSION.RELEASE}")
    } else {

    }

//    when (call.method) {
//
//
//
//      "getMessageFromFlutterView" -> {
////                cameraView?.getMessageFromFlutter(call.arguments.toString())
//        result.success(true)
//
//      }
//      "beginRecordVideo" -> {
//        result.success(cameraView?.startRecordVideo())
//
//      }
//
//      "endRecordVideo" -> {
//        result.success(cameraView?.stopRecordVideo())
//
//      }
//      "switchFrontCamera" -> {
//        result.success( cameraView?.switchFrontCamera())
//
//      }
//      "switchBackCamera" -> {
//        result.success(cameraView?.switchBackCamera())
//      }
//      else -> {
////                result.success()
//      }
//    }



  }

  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    channel.setMethodCallHandler(null)
  }


  companion object {
    // 通过唯一值id进行控件注册
    private const val VIEW_TYPE_ID = "com.kuafood.kms.android/camera"

    var activityBinding: ActivityPluginBinding? = null
  }

  override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    activityBinding = binding

  }

  override fun onDetachedFromActivityForConfigChanges() {

  }

  override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    activityBinding = binding
  }

  override fun onDetachedFromActivity() {
    activityBinding = null;
  }
}
